/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "dal/domain_participant.h"
#include "dal_cyclonedds/entity_impl/DalDomainParticipantImpl.hpp"
#include "dal_cyclonedds/converter/GuidConverter.hpp"
#include <dds/dds.h>

TEST(GuidConverter, ConvertGuidFromCycloneddsToDal)
{
    dal_domain_participant_t* participant = nullptr;
    participant = dal_create_domain_participant(20, DAL_PARTICIPANT_QOS_DEFAULT, nullptr, DAL_ANY_STATUS);
    EXPECT_NE(participant, nullptr);
    
    dds_entity_t c_participant;
    c_participant = (reinterpret_cast<DalDomainParticipantImpl*>(participant->handle_))->CycloneddsDomainParticipant();
    EXPECT_GT(c_participant, 0);
    
    dds_guid_t c_guid;
    EXPECT_EQ(dds_get_guid(c_participant, &c_guid), DDS_RETCODE_OK);
    
    ddsi_guid_t c_guid_s;
    memcpy(&c_guid_s, &c_guid, sizeof(ddsi_guid_t));
    
    dal_guid_t d_guid = dal_domain_participant_get_guid(participant);
    
    auto ret = memcmp(c_guid_s.prefix.s, d_guid.guidprefix.value, GUIDPREFIX_SIZE);
    EXPECT_EQ(0, ret);

    uint8_t c_entityid_value[ENTITYID_SIZE];
    memcpy(c_entityid_value, (uint8_t*)&c_guid_s.entityid.u, ENTITYID_SIZE);
    ret = memcmp(c_entityid_value, d_guid.entity_id.value, ENTITYID_SIZE);
    EXPECT_EQ(0, ret);
}
